package com.detu.ivos.user.service.impl;

import com.detu.ivos.base.exception.ServiceException;
import com.detu.ivos.base.response.StatusCode;
import com.detu.ivos.user.mapper.usermapper.UserMapper;
import com.detu.ivos.user.pojo.dto.UserLoginParam;
import com.detu.ivos.user.pojo.dto.UserQuery;
import com.detu.ivos.user.pojo.dto.UserSaveParam;
import com.detu.ivos.user.pojo.entity.User;
import com.detu.ivos.user.pojo.vo.UserVO;
import com.detu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录业务，参数：{}", userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if(userVO == null){
            //用户名错了,查无此人
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){
            //密码错了
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }
    //新增用户和编辑更新用户
    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        log.debug("保存用户业务，参数：{}", userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam, user);
        if(user.getId()==null){//新增******************不走这儿
            user.setPassword("123456");
            user.setCreateTime(new Date());
            log.debug("用户对象：{}", user);
            userMapper.insert(user);
        }else {
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }

    }
    //查询用户列表
    @Override
    public List<UserVO> queryUser(UserQuery userQuery) {
        log.debug("查询用户业务，参数：{}", userQuery);
        List<UserVO> listUserVO = userMapper.selectByUsernameAndStatus(userQuery);
        log.debug("查询出的列表：{}", listUserVO);
        return listUserVO;
    }

    //重置密码
    @Override
    public void resetPassword(Long userId) {
        log.debug("重置密码业务参数",userId);
        User user =new User();
        user.setId(userId);
        user.setPassword("root");
        user.setUpdateTime(new Date());
        userMapper.update(user);

    }

    @Override
    public void changeStatus(Long userId, String status) {
        log.debug("id",userId);
        log.debug("状态",status);
        User user =new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long userId) {
        int i = userMapper.deleteUserById(userId);
        if (i<=0){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
    }

    @Override
    public List<UserVO> selectAuditUserList(Long parentId) {
        //根据当前用户的领导id，查当前用户的上级领导的信息，保存到数组里
        ArrayList<UserVO> userVOList = new ArrayList<>();
        UserVO auditUser1=userMapper.selectById(parentId);
        userVOList.add(auditUser1);
        //如果有上级，并且上级领导的领导id字段不为空
        if (auditUser1!=null && auditUser1.getParentId() !=null){
            //如果上级还有上级领导，那就把上级领导的上级领导id再次作为查询条件，查到上上级领导信息，也存入数组中一起返回
            UserVO auditUser2 =userMapper.selectById(auditUser1.getParentId());
            userVOList.add(auditUser2);
        }
        return userVOList;
    }

}
